package com.rongke.damo.medicalrecord.controller;

import com.rongke.biz.service.MedicalRecordManager;
import com.rongke.biz.service.UserManager;
import com.rongke.commons.JsonResp;
import com.rongke.model.MedicalRecords;
import com.rongke.model.User;
import com.rongke.utils.StringUtil;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 病历管理大controller
 * 
 * @MedicalRecordsController
 * @病历Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value = "/api/damo/medicalRecords")
@Transactional
public class DamoMedicalRecordController {
    private static final Logger  log = Logger.getLogger(DamoMedicalRecordController.class);

    @Autowired
    private UserManager          userManager;

    @Autowired
    private MedicalRecordManager medicalRecordManager;

    /**
     * 根据登陆者ID查询病历基本信息
     * 
     * @param medicalRecords
     * @return
     */
    @RequestMapping(value = "/queryByPhone", method = RequestMethod.GET)
    public JsonResp queryByPhone(String phone) {
        log.debug("queryByPhone");

        try {
            if (StringUtil.isEmpty(phone)) {
                return JsonResp.fa(null);
            }

            User user = userManager.selectOrAddUser(phone);
            if (null == user) {
                return JsonResp.fa(null);
            }
            String userId = user.getUserId();

            MedicalRecords mrInfo = medicalRecordManager.selectOrAddMrByUserId(userId);
            if (null == mrInfo) {
                return JsonResp.fa(null);
            }
            return JsonResp.ok(mrInfo);
        } catch (Exception e) {
            log.error("queryByPhone failed", e);
            return JsonResp.fa(null);
        }
    }
}
